package com.leemon.mall.sys.controller;


import cn.hutool.core.map.MapUtil;
import com.leemon.mall.security.util.SecurityUtils;
import com.leemon.mall.sys.model.SysMenu;
import com.leemon.mall.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;


/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author leemon
 * @since 2019-09-04
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "后台-菜单接口", description = "SysMenuController")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/nav")
    @ApiOperation(value = "获取用户所拥有的菜单和权限", notes = "通过登陆用户的userId获取用户所拥有的菜单和权限")
    public ResponseEntity nav() {
        List<SysMenu> sysMenus = sysMenuService.listMenuByUserId(SecurityUtils.getSysUser().getUserId());
        Collection<GrantedAuthority> authorities = SecurityUtils.getSysUser().getAuthorities();
        return ResponseEntity.ok(
                MapUtil.builder()
                        .put("menuList", sysMenus)
                        .put("authorities", authorities)
                        .build());
    }

}

